package cn.com.bluemoon.daps.system.controller.api;

import cn.com.bluemoon.daps.api.sys.RemoteSysDeptService;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants.DapsApiSystem;
import cn.com.bluemoon.daps.common.datascope.IgnorePermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.toolkit.TreeUtils;
import cn.com.bluemoon.daps.system.controller.DapSystemDepartmentController;
import cn.com.bluemoon.daps.system.entity.DapSystemDepartment;
import cn.com.bluemoon.daps.system.entity.DapSystemWechatDept;
import cn.com.bluemoon.daps.system.service.DapSystemDepartmentService;
import cn.com.bluemoon.daps.system.service.DapSystemWechatDeptService;
import cn.com.bluemoon.daps.system.vo.DapSystemWechatDeptTreeVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Jarod.Kong
 */
@Api(tags = {"FeignClient服务-系统部门"})
@RequestMapping(DapsApiSystem.API_SYSDEPT)
@RestController
@Slf4j
@IgnorePermission(name = "服务注册-系统部门")
public class DapSysDeptApi implements RemoteSysDeptService {
    @Resource
    private DapSystemDepartmentController systemDepartmentController;

    @Resource
    private DapSystemDepartmentService systemDepartmentService;

    @Autowired
    private DapSystemWechatDeptService systemWechatDeptService;

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "获取部门信息")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ResultBean.class)})
    public ResultBean<DapSystemDepartment> getById(@PathVariable String id) {
        return systemDepartmentController.getById(id);
    }

    @GetMapping("/list")
    public Map<String, String> dbDistinctDepartAndIdMap() {
        return systemDepartmentService.dbDistinctDepartAndIdMap();
    }

    @GetMapping("/listAll")
    @ApiOperation(value = "获取部门信息")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ResultBean.class)})
    public ResultBean<List<DapSystemDepartment>> listAll() {
        return systemDepartmentController.list();
    }

    @Override
    public ResultBean<DapSystemWechatDept> getWechatDeptById(String id) {
        return ResultBean.ok(systemWechatDeptService.getById(id));
    }

    @Override
    public ResultBean<List<DapSystemWechatDept>> getWechatDeptByName(String name) {
        List<DapSystemWechatDept> res = systemWechatDeptService.list(Wrappers.<DapSystemWechatDept>lambdaQuery()
                .eq(DapSystemWechatDept::getName, name));
        return ResultBean.ok(res);
    }

    @Override
    public ResultBean<List<DapSystemWechatDeptTreeVo>> getWechatDeptList(Integer level) {
        List<DapSystemWechatDeptTreeVo> levelResult = systemWechatDeptService.getDapSystemWechatDeptTreeVos(level);
        return ResultBean.ok(levelResult);
    }
}
